var time = 0;
var score =0;
var scoreMark = score;
var mode = 0;
var level = 1;
var count =1;
var username ="";
var maxLevel = 3;
var numberQInMap = 10;
//var idSytemTabl = 0;
////////////////////////////
// random number beetween 0 to 13
var i ;
var numberQuestion = 0;
var listQA = new Array;

/////////////////// database
var db = openDatabaseSync("QDeclarativeExampleDB", "1.0", "The Example QML SQL!", 1000000);
// insert database
db.transaction(
    function(tx) {
                //tx.executeSql('DELETE FROM QATbl');

                var testQATbl = tx.executeSql('SELECT * FROM QATbl');
                if( testQATbl.rows.length === 0){

                    // Add (another) greeting row
                                        /////////////////////// mode easy
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 0,"Image/Question/q1.png",
                                                                                                        "Image/Question/a1_1.png",
                                                                                                        "Image/Question/a1_2.png",
                                                                                                        "Image/Question/a1_3.png",
                                                                                                        "Image/Question/a1_4.png",
                                                                                                        1,1,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 1,"Image/Question/q2.png",
                                                                                                        "Image/Question/a2_1.png",
                                                                                                        "Image/Question/a2_2.png",
                                                                                                        "Image/Question/a2_3.png",
                                                                                                        "Image/Question/a2_4.png",
                                                                                                        1,1,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 2,"Image/Question/q3.png",
                                                                                                        "Image/Question/a3_1.png",
                                                                                                        "Image/Question/a3_2.png",
                                                                                                        "Image/Question/a3_3.png",
                                                                                                        "Image/Question/a3_4.png",
                                                                                                        1,1,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 3,"Image/Question/q4.png",
                                                                                                        "Image/Question/a4_1.png",
                                                                                                        "Image/Question/a4_2.png",
                                                                                                        "Image/Question/a4_3.png",
                                                                                                        "Image/Question/a4_4.png",
                                                                                                        1,1,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 4,"Image/Question/q5.png",
                                                                                                        "Image/Question/a5_1.png",
                                                                                                        "Image/Question/a5_2.png",
                                                                                                        "Image/Question/a5_3.png",
                                                                                                        "Image/Question/a5_4.png",
                                                                                                        1,1,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 5,"Image/Question/q6.png",
                                                                                                        "Image/Question/a6_1.png",
                                                                                                        "Image/Question/a6_2.png",
                                                                                                        "Image/Question/a6_3.png",
                                                                                                        "Image/Question/a6_4.png",
                                                                                                        1,1,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 6,"Image/Question/q7.png",
                                                                                                        "Image/Question/a7_1.png",
                                                                                                        "Image/Question/a7_2.png",
                                                                                                        "Image/Question/a7_3.png",
                                                                                                        "Image/Question/a7_4.png",
                                                                                                        2 ,1,1]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 7,"Image/Question/q8.png",
                                                                                                        "Image/Question/a8_1.png",
                                                                                                        "Image/Question/a8_2.png",
                                                                                                        "Image/Question/a8_3.png",
                                                                                                        "Image/Question/a8_4.png",
                                                                                                        2 ,1,1]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 8,"Image/Question/q9.png",
                                                                                                        "Image/Question/a9_1.png",
                                                                                                        "Image/Question/a9_2.png",
                                                                                                        "Image/Question/a9_3.png",
                                                                                                        "Image/Question/a9_4.png",
                                                                                                        2 ,1,1]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 9,"Image/Question/q10.png",
                                                                                                        "Image/Question/a10_1.png",
                                                                                                        "Image/Question/a10_2.png",
                                                                                                        "Image/Question/a10_3.png",
                                                                                                        "Image/Question/a10_4.png",
                                                                                                        2 ,1,1]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 10,"Image/Question/q11.png",
                                                                                                        "Image/Question/a11_1.png",
                                                                                                        "Image/Question/a11_2.png",
                                                                                                        "Image/Question/a11_3.png",
                                                                                                        "Image/Question/a11_4.png",
                                                                                                        2,1,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 11,"Image/Question/q12.png",
                                                                                                        "Image/Question/a12_1.png",
                                                                                                        "Image/Question/a12_2.png",
                                                                                                        "Image/Question/a12_3.png",
                                                                                                        "Image/Question/a12_4.png",
                                                                                                        3,1,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 12,"Image/Question/q13.png",
                                                                                                        "Image/Question/a13_1.png",
                                                                                                        "Image/Question/a13_2.png",
                                                                                                        "Image/Question/a13_3.png",
                                                                                                        "Image/Question/a13_4.png",
                                                                                                        3,1,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 13,"Image/Question/q14.png",
                                                                                                        "Image/Question/a14_1.png",
                                                                                                        "Image/Question/a14_2.png",
                                                                                                        "Image/Question/a14_3.png",
                                                                                                        "Image/Question/a14_4.png",
                                                                                                        3,1,1 ]);
                    ///////////////////////lv 2 easy
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 0,"Image/Question/q15.png",
                                                                                                        "Image/Question/a15_1.png",
                                                                                                        "Image/Question/a15_2.png",
                                                                                                        "Image/Question/a15_3.png",
                                                                                                        "Image/Question/a15_4.png",
                                                                                                        3,2,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 1,"Image/Question/q16.png",
                                                                                                        "Image/Question/a16_1.png",
                                                                                                        "Image/Question/a16_2.png",
                                                                                                        "Image/Question/a16_3.png",
                                                                                                        "Image/Question/a16_4.png",
                                                                                                        3,2,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 2,"Image/Question/q17.png",
                                                                                                        "Image/Question/a17_1.png",
                                                                                                        "Image/Question/a17_2.png",
                                                                                                        "Image/Question/a17_3.png",
                                                                                                        "Image/Question/a17_4.png",
                                                                                                        3,2,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 3,"Image/Question/q18.png",
                                                                                                        "Image/Question/a18_1.png",
                                                                                                        "Image/Question/a18_2.png",
                                                                                                        "Image/Question/a18_3.png",
                                                                                                        "Image/Question/a18_4.png",
                                                                                                        4,2,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 4,"Image/Question/q19.png",
                                                                                                        "Image/Question/a19_1.png",
                                                                                                        "Image/Question/a19_2.png",
                                                                                                        "Image/Question/a19_3.png",
                                                                                                        "Image/Question/a19_4.png",
                                                                                                        4,2,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 5,"Image/Question/q20.png",
                                                                                                        "Image/Question/a20_1.png",
                                                                                                        "Image/Question/a20_2.png",
                                                                                                        "Image/Question/a20_3.png",
                                                                                                        "Image/Question/a20_4.png",
                                                                                                        4,2,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 6,"Image/Question/q21.png",
                                                                                                        "Image/Question/a21_1.png",
                                                                                                        "Image/Question/a21_2.png",
                                                                                                        "Image/Question/a21_3.png",
                                                                                                        "Image/Question/a21_4.png",
                                                                                                        4,2,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 7,"Image/Question/q22.png",
                                                                                                        "Image/Question/a22_1.png",
                                                                                                        "Image/Question/a22_2.png",
                                                                                                        "Image/Question/a22_3.png",
                                                                                                        "Image/Question/a22_4.png",
                                                                                                        4,2,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 8,"Image/Question/q23.png",
                                                                                                        "Image/Question/a23_1.png",
                                                                                                        "Image/Question/a23_2.png",
                                                                                                        "Image/Question/a23_3.png",
                                                                                                        "Image/Question/a23_4.png",
                                                                                                        4,2,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 9,"Image/Question/q24.png",
                                                                                                        "Image/Question/a24_1.png",
                                                                                                        "Image/Question/a24_2.png",
                                                                                                        "Image/Question/a24_3.png",
                                                                                                        "Image/Question/a24_4.png",
                                                                                                        4,2,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 10,"Image/Question/q25.png",
                                                                                                        "Image/Question/a25_1.png",
                                                                                                        "Image/Question/a25_2.png",
                                                                                                        "Image/Question/a25_3.png",
                                                                                                        "Image/Question/a25_4.png",
                                                                                                        4,2,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 11,"Image/Question/q26.png",
                                                                                                        "Image/Question/a26_1.png",
                                                                                                        "Image/Question/a26_2.png",
                                                                                                        "Image/Question/a26_3.png",
                                                                                                        "Image/Question/a26_4.png",
                                                                                                        4,2,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 12,"Image/Question/q27.png",
                                                                                                        "Image/Question/a27_1.png",
                                                                                                        "Image/Question/a27_2.png",
                                                                                                        "Image/Question/a27_3.png",
                                                                                                        "Image/Question/a27_4.png",
                                                                                                        4,2,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 13,"Image/Question/q28.png",
                                                                                                        "Image/Question/a28_1.png",
                                                                                                        "Image/Question/a28_2.png",
                                                                                                        "Image/Question/a28_3.png",
                                                                                                        "Image/Question/a28_4.png",
                                                                                                        1,2,1 ]);
                    ///// lv 3 easy
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 0,"Image/Question/q29.png",
                                                                                                        "Image/Question/a29_1.png",
                                                                                                        "Image/Question/a29_2.png",
                                                                                                        "Image/Question/a29_3.png",
                                                                                                        "Image/Question/a29_4.png",
                                                                                                        3,3,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 1,"Image/Question/q30.png",
                                                                                                        "Image/Question/a30_1.png",
                                                                                                        "Image/Question/a30_2.png",
                                                                                                        "Image/Question/a30_3.png",
                                                                                                        "Image/Question/a30_4.png",
                                                                                                        2,3,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 2,"Image/Question/q31.png",
                                                                                                        "Image/Question/a31_1.png",
                                                                                                        "Image/Question/a31_2.png",
                                                                                                        "Image/Question/a31_3.png",
                                                                                                        "Image/Question/a31_4.png",
                                                                                                        3,3,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 3,"Image/Question/q32.png",
                                                                                                        "Image/Question/a32_1.png",
                                                                                                        "Image/Question/a32_2.png",
                                                                                                        "Image/Question/a32_3.png",
                                                                                                        "Image/Question/a32_4.png",
                                                                                                        1,3,1 ]);

                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 4,"Image/Question/q33.png",
                                                                                                        "Image/Question/a33_1.png",
                                                                                                        "Image/Question/a33_2.png",
                                                                                                        "Image/Question/a33_3.png",
                                                                                                        "Image/Question/a33_4.png",
                                                                                                        1,3,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 5,"Image/Question/q34.png",
                                                                                                        "Image/Question/a34_1.png",
                                                                                                        "Image/Question/a34_2.png",
                                                                                                        "Image/Question/a34_3.png",
                                                                                                        "Image/Question/a34_4.png",
                                                                                                        1,3,1]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 6,"Image/Question/q35.png",
                                                                                                        "Image/Question/a35_1.png",
                                                                                                        "Image/Question/a35_2.png",
                                                                                                        "Image/Question/a35_3.png",
                                                                                                        "Image/Question/a35_4.png",
                                                                                                        2,3,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 7,"Image/Question/q36.png",
                                                                                                        "Image/Question/a36_1.png",
                                                                                                        "Image/Question/a36_2.png",
                                                                                                        "Image/Question/a36_3.png",
                                                                                                        "Image/Question/a36_4.png",
                                                                                                        2,3,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 8,"Image/Question/q37.png",
                                                                                                        "Image/Question/a37_1.png",
                                                                                                        "Image/Question/a37_2.png",
                                                                                                        "Image/Question/a37_3.png",
                                                                                                        "Image/Question/a37_4.png",
                                                                                                        2,3,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 9,"Image/Question/q38.png",
                                                                                                        "Image/Question/a38_1.png",
                                                                                                        "Image/Question/a38_2.png",
                                                                                                        "Image/Question/a38_3.png",
                                                                                                        "Image/Question/a38_4.png",
                                                                                                        3,3,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 10,"Image/Question/q39.png",
                                                                                                        "Image/Question/a39_1.png",
                                                                                                        "Image/Question/a39_2.png",
                                                                                                        "Image/Question/a39_3.png",
                                                                                                        "Image/Question/a39_4.png",
                                                                                                        4,3,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 11,"Image/Question/q40.png",
                                                                                                        "Image/Question/a40_1.png",
                                                                                                        "Image/Question/a40_2.png",
                                                                                                        "Image/Question/a40_3.png",
                                                                                                        "Image/Question/a40_4.png",
                                                                                                        4,3,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 12,"Image/Question/q41.png",
                                                                                                        "Image/Question/a41_1.png",
                                                                                                        "Image/Question/a41_2.png",
                                                                                                        "Image/Question/a41_3.png",
                                                                                                        "Image/Question/a41_4.png",
                                                                                                        4,3,1 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 13,"Image/Question/q42.png",
                                                                                                        "Image/Question/a42_1.png",
                                                                                                        "Image/Question/a42_2.png",
                                                                                                        "Image/Question/a42_3.png",
                                                                                                        "Image/Question/a42_4.png",
                                                                                                        1,3,1 ]);
                    //////////////////////////////////////// mode hard
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 43,"Image/Question/q43.png",
                                                                                                        "Image/Question/a43_1.png",
                                                                                                        "Image/Question/a43_2.png",
                                                                                                        "Image/Question/a43_3.png",
                                                                                                        "Image/Question/a43_4.png",
                                                                                                        2,1,3 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 44,"Image/Question/q44.png",
                                                                                                        "Image/Question/a44_1.png",
                                                                                                        "Image/Question/a44_2.png",
                                                                                                        "Image/Question/a44_3.png",
                                                                                                        "Image/Question/a44_4.png",
                                                                                                        1,1,3 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 45,"Image/Question/q45.png",
                                                                                                        "Image/Question/a45_1.png",
                                                                                                        "Image/Question/a45_2.png",
                                                                                                        "Image/Question/a45_3.png",
                                                                                                        "Image/Question/a45_4.png",
                                                                                                        3,1,3 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 46,"Image/Question/q46.png",
                                                                                                        "Image/Question/a46_1.png",
                                                                                                        "Image/Question/a46_2.png",
                                                                                                        "Image/Question/a46_3.png",
                                                                                                        "Image/Question/a46_4.png",
                                                                                                        2,1,3 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 47,"Image/Question/q47.png",
                                                                                                        "Image/Question/a47_1.png",
                                                                                                        "Image/Question/a47_2.png",
                                                                                                        "Image/Question/a47_3.png",
                                                                                                        "Image/Question/a47_4.png",
                                                                                                        1,1,3 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 48,"Image/Question/q48.png",
                                                                                                        "Image/Question/a48_1.png",
                                                                                                        "Image/Question/a48_2.png",
                                                                                                        "Image/Question/a48_3.png",
                                                                                                        "Image/Question/a48_4.png",
                                                                                                        1,1,3 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 49,"Image/Question/q49.png",
                                                                                                        "Image/Question/a49_1.png",
                                                                                                        "Image/Question/a49_2.png",
                                                                                                        "Image/Question/a49_3.png",
                                                                                                        "Image/Question/a49_4.png",
                                                                                                        1,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 50,"Image/Question/q50.png",
                                                                                                        "Image/Question/a50_1.png",
                                                                                                        "Image/Question/a50_2.png",
                                                                                                        "Image/Question/a50_3.png",
                                                                                                        "Image/Question/a50_4.png",
                                                                                                        1,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 51,"Image/Question/q51.png",
                                                                                                        "Image/Question/a51_1.png",
                                                                                                        "Image/Question/a51_2.png",
                                                                                                        "Image/Question/a51_3.png",
                                                                                                        "Image/Question/a51_4.png",
                                                                                                        1,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 52,"Image/Question/q52.png",
                                                                                                        "Image/Question/a52_1.png",
                                                                                                        "Image/Question/a52_2.png",
                                                                                                        "Image/Question/a52_3.png",
                                                                                                        "Image/Question/a52_4.png",
                                                                                                        1,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 53,"Image/Question/q53.png",
                                                                                                        "Image/Question/a53_1.png",
                                                                                                        "Image/Question/a53_2.png",
                                                                                                        "Image/Question/a53_3.png",
                                                                                                        "Image/Question/a53_4.png",
                                                                                                        1,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 54,"Image/Question/q54.png",
                                                                                                        "Image/Question/a54_1.png",
                                                                                                        "Image/Question/a54_2.png",
                                                                                                        "Image/Question/a54_3.png",
                                                                                                        "Image/Question/a54_4.png",
                                                                                                        1,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 55,"Image/Question/q55.png",
                                                                                                        "Image/Question/a55_1.png",
                                                                                                        "Image/Question/a55_2.png",
                                                                                                        "Image/Question/a55_3.png",
                                                                                                        "Image/Question/a55_4.png",
                                                                                                        1,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 56,"Image/Question/q56.png",
                                                                                                        "Image/Question/a56_1.png",
                                                                                                        "Image/Question/a56_2.png",
                                                                                                        "Image/Question/a56_3.png",
                                                                                                        "Image/Question/a56_4.png",
                                                                                                        1,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 57,"Image/Question/q57.png",
                                                                                                        "Image/Question/a57_1.png",
                                                                                                        "Image/Question/a57_2.png",
                                                                                                        "Image/Question/a57_3.png",
                                                                                                        "Image/Question/a57_4.png",
                                                                                                        2,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 58,"Image/Question/q58.png",
                                                                                                        "Image/Question/a58_1.png",
                                                                                                        "Image/Question/a58_2.png",
                                                                                                        "Image/Question/a58_3.png",
                                                                                                        "Image/Question/a58_4.png",
                                                                                                        2,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 59,"Image/Question/q59.png",
                                                                                                        "Image/Question/a59_1.png",
                                                                                                        "Image/Question/a59_2.png",
                                                                                                        "Image/Question/a59_3.png",
                                                                                                        "Image/Question/a59_4.png",
                                                                                                        2,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 60,"Image/Question/q60.png",
                                                                                                        "Image/Question/a60_1.png",
                                                                                                        "Image/Question/a60_2.png",
                                                                                                        "Image/Question/a60_3.png",
                                                                                                        "Image/Question/a60_4.png",
                                                                                                        2,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 61,"Image/Question/q61.png",
                                                                                                        "Image/Question/a61_1.png",
                                                                                                        "Image/Question/a61_2.png",
                                                                                                        "Image/Question/a61_3.png",
                                                                                                        "Image/Question/a61_4.png",
                                                                                                        2,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 62,"Image/Question/q62.png",
                                                                                                        "Image/Question/a62_1.png",
                                                                                                        "Image/Question/a62_2.png",
                                                                                                        "Image/Question/a62_3.png",
                                                                                                        "Image/Question/a62_4.png",
                                                                                                        2,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 63,"Image/Question/q63.png",
                                                                                                        "Image/Question/a63_1.png",
                                                                                                        "Image/Question/a63_2.png",
                                                                                                        "Image/Question/a63_3.png",
                                                                                                        "Image/Question/a63_4.png",
                                                                                                        3,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 64,"Image/Question/q64.png",
                                                                                                        "Image/Question/a64_1.png",
                                                                                                        "Image/Question/a64_2.png",
                                                                                                        "Image/Question/a64_3.png",
                                                                                                        "Image/Question/a64_4.png",
                                                                                                        3,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 65,"Image/Question/q65.png",
                                                                                                        "Image/Question/a65_1.png",
                                                                                                        "Image/Question/a65_2.png",
                                                                                                        "Image/Question/a65_3.png",
                                                                                                        "Image/Question/a65_4.png",
                                                                                                        3,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 66,"Image/Question/q66.png",
                                                                                                        "Image/Question/a66_1.png",
                                                                                                        "Image/Question/a66_2.png",
                                                                                                        "Image/Question/a66_3.png",
                                                                                                        "Image/Question/a66_4.png",
                                                                                                        3,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 67,"Image/Question/q67.png",
                                                                                                        "Image/Question/a67_1.png",
                                                                                                        "Image/Question/a67_2.png",
                                                                                                        "Image/Question/a67_3.png",
                                                                                                        "Image/Question/a67_4.png",
                                                                                                        3,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 68,"Image/Question/q68.png",
                                                                                                        "Image/Question/a68_1.png",
                                                                                                        "Image/Question/a68_2.png",
                                                                                                        "Image/Question/a68_3.png",
                                                                                                        "Image/Question/a68_4.png",
                                                                                                        4,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 69,"Image/Question/q69.png",
                                                                                                        "Image/Question/a69_1.png",
                                                                                                        "Image/Question/a69_2.png",
                                                                                                        "Image/Question/a69_3.png",
                                                                                                        "Image/Question/a69_4.png",
                                                                                                        4,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 70,"Image/Question/q70.png",
                                                                                                        "Image/Question/a70_1.png",
                                                                                                        "Image/Question/a70_2.png",
                                                                                                        "Image/Question/a70_3.png",
                                                                                                        "Image/Question/a70_4.png",
                                                                                                        4,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 71,"Image/Question/q71.png",
                                                                                                        "Image/Question/a71_1.png",
                                                                                                        "Image/Question/a71_2.png",
                                                                                                        "Image/Question/a71_3.png",
                                                                                                        "Image/Question/a71_4.png",
                                                                                                        4,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 72,"Image/Question/q72.png",
                                                                                                        "Image/Question/a72_1.png",
                                                                                                        "Image/Question/a72_2.png",
                                                                                                        "Image/Question/a72_3.png",
                                                                                                        "Image/Question/a72_4.png",
                                                                                                        4,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 73,"Image/Question/q73.png",
                                                                                                        "Image/Question/a73_1.png",
                                                                                                        "Image/Question/a73_2.png",
                                                                                                        "Image/Question/a73_3.png",
                                                                                                        "Image/Question/a73_4.png",
                                                                                                        4,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 74,"Image/Question/q74.png",
                                                                                                        "Image/Question/a74_1.png",
                                                                                                        "Image/Question/a74_2.png",
                                                                                                        "Image/Question/a74_3.png",
                                                                                                        "Image/Question/a74_4.png",
                                                                                                        4,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 75,"Image/Question/q75.png",
                                                                                                        "Image/Question/a75_1.png",
                                                                                                        "Image/Question/a75_2.png",
                                                                                                        "Image/Question/a75_3.png",
                                                                                                        "Image/Question/a75_4.png",
                                                                                                        4,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 76,"Image/Question/q76.png",
                                                                                                        "Image/Question/a76_1.png",
                                                                                                        "Image/Question/a76_2.png",
                                                                                                        "Image/Question/a76_3.png",
                                                                                                        "Image/Question/a76_4.png",
                                                                                                        4,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 77,"Image/Question/q77.png",
                                                                                                        "Image/Question/a77_1.png",
                                                                                                        "Image/Question/a77_2.png",
                                                                                                        "Image/Question/a77_3.png",
                                                                                                        "Image/Question/a77_4.png",
                                                                                                        1,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 78,"Image/Question/q78.png",
                                                                                                        "Image/Question/a78_1.png",
                                                                                                        "Image/Question/a78_2.png",
                                                                                                        "Image/Question/a78_3.png",
                                                                                                        "Image/Question/a78_4.png",
                                                                                                        1,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 79,"Image/Question/q79.png",
                                                                                                        "Image/Question/a79_1.png",
                                                                                                        "Image/Question/a79_2.png",
                                                                                                        "Image/Question/a79_3.png",
                                                                                                        "Image/Question/a79_4.png",
                                                                                                        1,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 80,"Image/Question/q80.png",
                                                                                                        "Image/Question/a80_1.png",
                                                                                                        "Image/Question/a80_2.png",
                                                                                                        "Image/Question/a80_3.png",
                                                                                                        "Image/Question/a80_4.png",
                                                                                                        1,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 81,"Image/Question/q81.png",
                                                                                                        "Image/Question/a81_1.png",
                                                                                                        "Image/Question/a81_2.png",
                                                                                                        "Image/Question/a81_3.png",
                                                                                                        "Image/Question/a81_4.png",
                                                                                                        2,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 82,"Image/Question/q82.png",
                                                                                                        "Image/Question/a82_1.png",
                                                                                                        "Image/Question/a82_2.png",
                                                                                                        "Image/Question/a82_3.png",
                                                                                                        "Image/Question/a82_4.png",
                                                                                                        2,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 83,"Image/Question/q83.png",
                                                                                                        "Image/Question/a83_1.png",
                                                                                                        "Image/Question/a83_2.png",
                                                                                                        "Image/Question/a83_3.png",
                                                                                                        "Image/Question/a83_4.png",
                                                                                                        2,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 84,"Image/Question/q84.png",
                                                                                                        "Image/Question/a84_1.png",
                                                                                                        "Image/Question/a84_2.png",
                                                                                                        "Image/Question/a84_3.png",
                                                                                                        "Image/Question/a84_4.png",
                                                                                                        3,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 85,"Image/Question/q85.png",
                                                                                                        "Image/Question/a85_1.png",
                                                                                                        "Image/Question/a85_2.png",
                                                                                                        "Image/Question/a85_3.png",
                                                                                                        "Image/Question/a85_4.png",
                                                                                                        3,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 86,"Image/Question/q86.png",
                                                                                                        "Image/Question/a86_1.png",
                                                                                                        "Image/Question/a86_2.png",
                                                                                                        "Image/Question/a86_3.png",
                                                                                                        "Image/Question/a86_4.png",
                                                                                                        4,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 87,"Image/Question/q87.png",
                                                                                                        "Image/Question/a87_1.png",
                                                                                                        "Image/Question/a87_2.png",
                                                                                                        "Image/Question/a87_3.png",
                                                                                                        "Image/Question/a87_4.png",
                                                                                                        4,1,2 ]);
                                        tx.executeSql('INSERT INTO QATbl VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 88,"Image/Question/q88.png",
                                                                                                        "Image/Question/a88_1.png",
                                                                                                        "Image/Question/a88_2.png",
                                                                                                        "Image/Question/a88_3.png",
                                                                                                        "Image/Question/a88_4.png",
                                                                                                        3,1,2 ]);
                }
                // Show all added greetings

               /* var rs = tx.executeSql('SELECT * FROM QATbl');

                for(var i = 0; i < rs.rows.length; i++) {
                    listQA[i] = rs.rows.item(i).ID + "*" + rs.rows.item(i).Question + "*" + rs.rows.item(i).Answer1
                    + "*" + rs.rows.item(i).Answer2 + "*" +rs.rows.item(i).Answer3 +"*" +rs.rows.item(i).Answer4 +"*" +rs.rows.item(i).Correct
                            }
                            */
    }
)
// highscore
function dataList(){
    var numberR;
    var listH = new Array;
    db.transaction(
        function(tx) {
            // Show all added greetings
            var rs = tx.executeSql('SELECT * FROM (SELECT * FROM HighScoreTbl LIMIT 3 ) ORDER BY Score DESC');
            numberR= rs.rows.length;
            for(var i = 0; i < rs.rows.length; i++) {
                listH[i] = rs.rows.item(i).Mode + "*" + rs.rows.item(i).Score + "*" + rs.rows.item(i).Name;
            }
        }
    )

    clearList();
    for(var t =0 ; t < numberR; t++){
        var qA = listH[t].split('*');
        if(qA[0]==="1"){
            addList(qA[2],qA[1],"easy");
        }
        else if(qA[0]==="2"){
            addList(qA[2],qA[1],"medium");
        }
        else if(qA[0]==="3"){
            addList(qA[2],qA[1],"hard");
        }
   }
}

function clearList(){
    listModel.clear();
}

function addList(name,score,mode){
    listModel.append({"name":name,"score":score,"mode":mode});
}
function hideMenuStart(){
    if(flagSound == false){
        startMenuWindow.state = "startMenuWindowHideNoSound"
    }
    else{
        startMenuWindow.state = "startMenuWindowHide"
    }
    start.flagStartMenuHide = 1
}

function showMenuStart(){
    if(start.flagSound == true){
        if(parent.width < parent.height)
            startMenuWindow.state ="startMenuWindowPortrait"
        else
            startMenuWindow.state ="startMenuWindowLandscape"
        start.flagStartMenuHide = 0
    }
    else{
        if(parent.width < parent.height)
            startMenuWindow.state ="startMenuWindowPortraitNoSound"
        else
            startMenuWindow.state ="startMenuWindowLandscapeNoSound"
        start.flagStartMenuHide = 0
    }
}
function restartMusic(){
    audioStart.stop();
    audioStart.play();
}

function addHighScore(){
    var flag = 0;
    var highscore;
    var modescore;
    var namePlayer;
    db.transaction(
        function(tx){
            var testSysTbl = tx.executeSql('SELECT * FROM SystemTbl');
            if( testSysTbl.rows.length !== 0){
                flag = 1;
                var noRo = testSysTbl.rows.length;
                highscore = testSysTbl.rows.item(noRo-1).Score;
                modescore = testSysTbl.rows.item(noRo-1).Mode;
                namePlayer = testSysTbl.rows.item(noRo-1).Name;
            }
        }
    )
    if(flag == 1){
        db.transaction(
            function(tx){
                tx.executeSql('INSERT INTO HighScoreTbl VALUES(?, ?, ?)', [ modescore, highscore,namePlayer]);
            }
        )
    }
}

function checkContinue(){
    var numberRo1 = 0;
    db.transaction(
        function(tx){
            var testSysTbl = tx.executeSql('SELECT * FROM SystemTbl');
            numberRo1 = testSysTbl.rows.length;
        }
    )
    return numberRo1;
}

function continueGame(){
    // load database system to restore state
    db.transaction(
        function(tx){
            var testSysTbl = tx.executeSql('SELECT * FROM SystemTbl');
            if( testSysTbl.rows.length !== 0){
                var numberRo = testSysTbl.rows.length;
                time = testSysTbl.rows.item(numberRo-1).Time;
                count = testSysTbl.rows.item(numberRo-1).Count;
                score = testSysTbl.rows.item(numberRo-1).Score;
                mode = testSysTbl.rows.item(numberRo-1).Mode;
                level = testSysTbl.rows.item(numberRo-1).Level;
                username = testSysTbl.rows.item(numberRo-1).Name;
                scoreMark = score;
            }
        }
    )
    // load map game

    loadMapGame();
    i = randomi();
    change(i);

}

function assignName(name){
    username = name;
}

function modeIndex(index){
    mode = index;
    // load map game
    loadMapGame();
    i = randomi();
    change(i);
    timer.start();
}

function saveState(){
    easymodeConclusion.state = "easymodeConclusionHide";
    startMenuWindow.state = "startMenuWindowHideNoSound"
    easymodeConclusion.flagHideEasymodeConclusion = 1;
    nextLevel();
    timer.stop();
    db.transaction(
        function(tx) {
            // Show all added greetings
            tx.executeSql('INSERT INTO SystemTbl VALUES(?, ?, ?, ?, ?, ?)', [level,mode,score,count,time,username]);
        }
    )
}

function playAgain(){
    time =0;
    buttonNext.text="Next";
    timerText.text ="Time: 0"
    // load map game
    loadMapGame();
    score = scoreMark;
    count = 1;
    scoreText.text = "Score: "+ scoreMark;
    i = randomi();
    change(i);
}

function nextLevelProcess(){
    time =0;
    buttonNext.text="Next";
    timerText.text ="Time: 0"
    level ++;
    // load map game
    loadMapGame();
    count = 1;
    i = randomi();
    change(i);
    scoreMark = score;
}

function conclusionPage(){
    scoreTextEasyMode.text = "Your score: " + score;
    if(level < maxLevel){
        if (score < level*5){
            buttonNextLevel.text ="Play again";
        }
        else{
            buttonNextLevel.text ="Next Level";
        }
    }
    else{
        buttonNextLevel.text ="Finish";
    }
}

function nextLevel(){
    if(buttonNextLevel.text === "Play again"){
        hideEasyConclusion();
        showEasyMode();
        playAgain();
    }
    else if(buttonNextLevel.text === "Next Level"){ 
        hideEasyConclusion();
        showEasyMode();
        nextLevelProcess();
    }

}

function nextPage(){
    if (buttonNext.text=="Next"){
        if(count >= numberQInMap -1){
            buttonNext.text="Finish"
        }
        checkChoice(i);
        iup();
        change(i);
    }
    else{
        checkChoice(i);
        easymodeWindow.state = "easymodeHide";
        easymodeWindow.flagHideEasymode = 1;

        if(easymodeConclusion.width < easymodeConclusion.height)
            easymodeConclusion.state ="easymodeConclusionPortrait";
        else
            easymodeConclusion.state ="easymodeConclusionLandscape";
        easymodeConclusion.flagHideEasymodeConclusion = 0;
        conclusionPage()
    }
}

function nextTime(){
    time ++;
    timerText.text = "Time: " + time.toString();
    checkTimer();
}

function checkTimer(){
    if(count <= numberQInMap -1){
        if(time > count*10){
            checkChoice(i);
            iup();
            change(i);
        }
    }
    else{
        if(time > count*10){
            easymodeWindow.state = "easymodeHide";
            easymodeWindow.flagHideEasymode = 1;
            if(easymodeConclusion.width < easymodeConclusion.height)
                easymodeConclusion.state ="easymodeConclusionPortrait";
            else
                easymodeConclusion.state ="easymodeConclusionLandscape";
            easymodeConclusion.flagHideEasymodeConclusion = 0;
            conclusionPage()
        }
    }
}

function change(t){
    var qA = listQA[t].split('*');
    imageQuestion.source = qA[1];
    imageA1.source = qA[2];
    imageA2.source = qA[3];
    imageA3.source = qA[4];
    imageA4.source = qA[5];

}

function checkChoice(t){
    var qA = listQA[t].split('*');
    var cor = qA[6]
    right.stop();
    if(button1.checked){
        if(cor === '1'){
            score++;
            right.play();
        }
    }
    if(button2.checked){
        if(cor === '2'){
            score++;
            right.play();
        }
    }
    if(button3.checked){
        if(cor === '3'){
            score++;
            right.play();
        }
    }
    if(button4.checked){
        if(cor === '4'){
            score++;
            right.play();
        }
    }
    scoreText.text = "Score: "+score.toString();
}

function iup(){
    count++;
    i = (i+1)% 14;
}

function choiceMap(l){
    startWindow.state = "startWindowHide"
    startWindow.flagHideStart = 1

    easymodeWindow.flagHideEasymode = 0
    if(startWindow.width < startWindow.height)
        easymodeWindow.state ="easymodePortrait"
    else
        easymodeWindow.state ="easymodeLandscape"
    modeIndex(l);
}

function showEasyMode(){
    easymodeWindow.flagHideEasymode = 0;
    if(easymodeWindow.width < easymodeWindow.height)
        easymodeWindow.state ="easymodePortrait";
    else
        easymodeWindow.state ="easymodeLandscape";
}

function hideEasyConclusion(){
    easymodeConclusion.state = "easymodeConclusionHide";
    easymodeConclusion.flagHideEasymodeConclusion = 1;
}

function loadMapGame(){
    db.transaction(
        function(tx) {
            // Show all added greetings
            var rs = tx.executeSql('SELECT * FROM QATbl WHERE Level ='+level +' AND Mode ='+ mode);
            numberQuestion = rs.rows.length;
            for(var i = 0; i < rs.rows.length; i++) {
                listQA[i] = rs.rows.item(i).ID + "*" + rs.rows.item(i).Question + "*" + rs.rows.item(i).Answer1
                + "*" + rs.rows.item(i).Answer2 + "*" +rs.rows.item(i).Answer3 +"*" +rs.rows.item(i).Answer4 +"*" +rs.rows.item(i).Correct
            }
        }
    )
}

function randomi() {
    return Math.floor(Math.random()*14);
}
